package com.vinaprime.primeschedule.businesslayer;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPContact_DL;
import com.vinaprime.primeschedule.entities.VPContact;

@SuppressLint("DefaultLocale")
public class VPContact_BL {
	private VPContact_DL myContactDL ;
	
	public VPContact_BL (Context context , String path){
		this.myContactDL = new VPContact_DL(context, path);
	}

	public int getNewContactId (){
		
		int result = 0;
		Cursor cur =  myContactDL.getNewContactID();
		if (cur.moveToFirst())
		{
			if (!cur.isNull(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcId()))) {
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcId())));
			}
		}
		return result+1;
	}

	public VPContact getContactbyID (int Id){
		VPContact myContact = new VPContact();
		Cursor cur  =  this.myContactDL.getContactbyId(Id);
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{			
				
				myContact.setVpcID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcId()))));
				myContact.setVpcCode(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcCode())));
				myContact.setVpcLastName(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcLastname())));
				myContact.setVpcFistName(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcFirstname())));				
				myContact.setVpcEmail(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail())));
				myContact.setVpcEmail1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail1())));
				myContact.setVpcEmail2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail2())));
				myContact.setVpcHandPhone(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone())));
				myContact.setVpcHandPhone1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone1())));
				myContact.setVpcHandPhone2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone2())));
				myContact.setVpcHomePhone(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone())));
				myContact.setVpcHomePhone1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone1())));
				myContact.setVpcHomePhone2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone2())));
				myContact.setVpcDescription(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcDescription())));
				myContact.setVpcEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEnableFlag())));
				myContact.setVpcImage(cur.getBlob(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcImage())));
				cur.moveToNext();
			}
		}
		return myContact;		
	}
	
	public List<VPContact> getAllContact() throws IllegalArgumentException, ParseException
	{
		List<VPContact> listContact = new ArrayList<VPContact>();
		
		Cursor cur  = this.myContactDL.getAllContact();
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
				
				VPContact mycontact = new VPContact();
				mycontact.setVpcID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcId()))));
				mycontact.setVpcCode(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcCode())));
				mycontact.setVpcLastName(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcLastname())));
				mycontact.setVpcFistName(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcFirstname())));				
				mycontact.setVpcEmail(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail())));
				mycontact.setVpcEmail1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail1())));
				mycontact.setVpcEmail2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEmail2())));
				mycontact.setVpcHandPhone(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone())));
				mycontact.setVpcHandPhone1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone1())));
				mycontact.setVpcHandPhone2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHandphone2())));
				mycontact.setVpcHomePhone(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone())));
				mycontact.setVpcHomePhone1(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone1())));
				mycontact.setVpcHomePhone2(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcHomephone2())));
				mycontact.setVpcDescription(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcDescription())));
				mycontact.setVpcEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcEnableFlag())));
				mycontact.setVpcImage(cur.getBlob(cur.getColumnIndexOrThrow(VPContact_DL.getDbVpcImage())));
				listContact.add(mycontact);
				cur.moveToNext();
			}
		}
		return listContact;
	}
	public Cursor getAllContactCursor(){
		Cursor cur  =  this.myContactDL.getAllContact();
		return cur;
	}
	public Cursor fetchAllContactsForGroup(){
		Cursor cur  =  this.myContactDL.fetchAllContactsForGroup();
		return cur;
	}
	
	public void addContact(VPContact myContact){
		this.myContactDL.AddContact(myContact);
	}
	public void updateContact(VPContact myContact){
		this.myContactDL.updateContact(myContact);
	}
	public void deleteContact(VPContact myContact){
		this.myContactDL.deleteContact(myContact);
	}
	public void deleteContactbyID(int _Vpc_Id){
		this.myContactDL.deleteContactbyID(_Vpc_Id);
	}

	
	@SuppressLint("DefaultLocale")
	public List<VPContact> getContactInContains(String _contains, List<VPContact> _listContact) throws IllegalArgumentException, ParseException
	{
		
		
		if(_contains =="" ||_contains ==null )
		{
			return _listContact;
		}
		else
		{
			List<VPContact> listContact = new ArrayList<VPContact>();
			
			int rowcount = _listContact.size();
			
			for (int i=0;i<rowcount;i++)
				{	
					String _str = _listContact.get(i).getVpcLastName() +" "+_listContact.get(i).getVpcFistName();
					if (_str.toLowerCase().contains(_contains.toLowerCase()))
					{
						VPContact mycontact = new VPContact();
						mycontact = _listContact.get(i);
						listContact.add(mycontact);
						
					}
			
				}
			return listContact;
		}
		
	}
}
